package hellfire.system.graphics;

import hellfire.system.R;

import java.util.Vector;

import android.opengl.GLES20;
import android.util.Log;

public class GLStringList implements GLDrawable
{
	protected Vector<GLString> strings = new Vector<GLString>();
	protected GLVec2 pos, dim;
	
	protected int font = R.drawable.basefont;
	protected float padding;
	protected float fontSize;
	protected float spacing;
	
	protected GLColor textColor = GLColor.WHITE;
	protected GLColor textBackground = new GLColor(1,1,1,0);
	
	public GLStringList(GLVec2 pos, GLVec2 dim, float fontSize)
	{
		this.pos = pos;
		this.dim = dim;
		padding = fontSize;
		this.fontSize = fontSize;
		this.spacing = fontSize/2;
	}

	public void add(String str)
	{
		GLString obj = new GLString(font, str, new GLVec2(pos.x() - dim.x() + padding, 0), fontSize);
		obj.setTextColor(textColor);
		obj.setBackgroundColor(textBackground);
		strings.add(obj);
		resetPositions();
	}
	
	public void add(String str, int index)
	{
		GLString obj = new GLString(font, str, new GLVec2(pos.x() - dim.x() + padding, 0), fontSize);
		obj.setTextColor(textColor);
		obj.setBackgroundColor(textBackground);
		strings.add(index, obj);
		resetPositions();
	}
	
	public int getNumVisibleElements()
	{
		float entryHeight = fontSize + spacing;
		return (int) (dim.y()*2 / entryHeight);
	}
	
	private void resetPositions()
	{
		float entryHeight = fontSize + spacing;
		float cur_y = pos.y() + dim.y() - fontSize/2 - padding;
		for(GLString str : strings)
		{
			str.setPos(new GLVec2(str.getPos().x(), cur_y));
			cur_y -= entryHeight;
		}
	}
	
	public void draw(float[] matrix) 
	{
		GLRenderer.pushMatrix(matrix);
		GLRenderer.setScissorBox(pos, dim);
		for(GLString str : strings)
		{
			//Log.d("R", "draw");
			str.draw(matrix);
		}
		GLRenderer.setScissorBox(new GLRelVec2(0,0), new GLRelVec2(1,1));
		GLRenderer.popMatrix(matrix);
	}

	@Override
	public GLVec2 getPos() 
	{
		return pos;
	}

	@Override
	public void setPos(GLVec2 pos) 
	{
		this.pos = pos;
		resetPositions();
	}

}
